/*
 * Copyright (c) 2020 - present, Inspur Genersoft Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.inspur.edp.cef.generator.configGenerator;

import com.inspur.edp.cef.entity.config.CefConfig;
import com.inspur.edp.cef.entity.config.MgrConfig;
import com.inspur.edp.cef.entity.config.RepositoryConfig;
import com.inspur.edp.cef.generator.DotNetToJavaStringHelper;
import com.inspur.edp.cef.generator.overall.CefGeneratorContext;

public abstract class CefConfigGenerator {
  protected CefGeneratorContext genCtx;

  public CefConfigGenerator(CefGeneratorContext genCtx) {
    this.genCtx = genCtx;
//    genCtx.getCefCoreGenCtx().setConfig(getGenConfig());
  }

  protected CefConfigGenerator() {}

  protected abstract String getConfigFileName();

  public final void generate() {
    CefConfig genConfig = getGenConfig();

    // ConfigSection configSection = new ConfigSection { CefConfigs = new List<CefConfig>(1) {
    // genConfig } }; ;

    // var binConfigPath = Path.Combine(PathTransform.GetUserProjectsRoot(),
    //   GenUtils.GetBinPath(genCtx.Metadata.RelativePath), GetConfigFileName());
    // if (File.Exists(binConfigPath))
    // {
    //    try
    //    {
    //        var existingConfig = CefConfigUtils.ReadConfigSection(binConfigPath);
    //        //删除以前生成过的配置
    //        var existing = existingConfig.CefConfigs.FirstOrDefault(item =>
    // string.Equals(item.getID(), genConfig.getID()));
    //        if (existing != null)
    //            existingConfig.CefConfigs.Remove(existing);
    //        existingConfig.CefConfigs.add(genConfig);//把新的config节加进配置
    //        configSection = existingConfig;
    //    }
    //    catch
    //    { }
    // }
    // CefConfigUtils.WriteConfigSection(Path.Combine(genCtx.GeneratePath, GetConfigFileName()),
    // configSection);//将配置写回文件
  }

  protected abstract String getConfigID();

  protected String getDotnetConfigID() {
    return null;
  }
  protected String getBEID() {
    return null;
  }

  protected abstract String getSourceID();

  protected abstract String getDefaultNamespace();

  public final CefConfig getGenConfig() {

    CefConfig tempVar = new CefConfig();
    tempVar.setBEID(getBEID());
    tempVar.setID(getConfigID());
    tempVar.setDotnetID(getDotnetConfigID());
    tempVar.setSourceID(getSourceID());
    tempVar.setDefaultNamespace(getDefaultNamespace());
    tempVar.setMgrConfig(getMgrConfig());
    tempVar.setRepositoryConfig(getRepositoryConfig());
    return tempVar;
  }

  private MgrConfig getMgrConfig() {
    MgrConfig tempVar = new MgrConfig();
    tempVar.setAllInterfaceAssembly(getApiAssemblyName());
    tempVar.setAssembly(getApiAssemblyName());
    tempVar.setAllInterfaceClassName(genCtx.getMgrAllInterfaceType().getFullName());
    tempVar.setClass(genCtx.getMgrInterfaceType().getFullName());
    tempVar.setImpAssembly(getCoreAssemblyName());
    tempVar.setImpClass(genCtx.getMgrImplType().getFullName());
    return tempVar;
  }

  protected abstract String getApiAssemblyName();

  protected abstract String getCoreAssemblyName();

  private RepositoryConfig getRepositoryConfig() {
    if (genCtx.getRootRepositoryType()==null||DotNetToJavaStringHelper.isNullOrEmpty(genCtx.getRootRepositoryType().getFullName())) {
      return null;
    }
    RepositoryConfig tempVar = new RepositoryConfig();
    tempVar.setAssembly(getCoreAssemblyName());
    tempVar.setClass(genCtx.getRootRepositoryType().getFullName());
    return tempVar;
    // return genCtx.RepopsitoryAssembly == null ? null :
    //    new RepositoryConfig()
    //    {
    //        Assembly = genCtx.RepopsitoryAssembly.getFullName(),
    //        Class = genCtx.RepoAssemblerType.getFullName()
    //    };
  }
}
